package com.edu.edumanager.controller;

import com.edu.edumanager.bean.EduChapter;
import com.edu.edumanager.service.ChapterService;
import com.edu.edumanager.vo.EduResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @program: edu-manager
 * @description:
 * @author: Jaime
 * @create: 2021-08-21 23:15
 **/
@RestController
@CrossOrigin
@RequestMapping("/eduservice/chapter")
public class EduChapterController {
    @Autowired
    ChapterService chapterService;
    /**
     * 添加章节
     * @param
     * @return 添加条数
     */
    @RequestMapping("/addChapter")
    public EduResult addChapter(@Valid @RequestBody EduChapter chapter) {
        int id = chapterService.addChapter(chapter);
        return EduResult.ok();
    };

    /**
     * 查询章节列表
     * @return list<EduChapter></>
     */
    @RequestMapping("/getChapterById/{chapterId}")
    public EduResult getChapterById(@PathVariable Integer chapterId){
        EduChapter chapter = chapterService.getChapterById(chapterId);
        return  EduResult.ok().data("chapter",chapter);
    };
    /**
     * 获取到章节小结 table的树形结构
     * @param courseId
     * @return
     */
    @RequestMapping("/getChapterList/{courseId}")
    public  EduResult getChapterList(@PathVariable Integer courseId){
        List<EduChapter> chapterLists = chapterService.getChapterList(courseId);
        return  EduResult.ok().data("chapterLists",chapterLists);
    };

    /**
     * 删除章节小结
     * @param chapterId
     * @return
     */
    @RequestMapping("/deleteChapter/{chapterId}")
    public EduResult deleteChapter(@PathVariable Integer chapterId){
        int count = chapterService.deleteChapterById(chapterId);
        return EduResult.ok().data("count",count);
    }

    /**
     * 修改章节
     * @param chapter
     * @return
     */
    @RequestMapping("/updateChapter")
    public EduResult updateChapter(@RequestBody EduChapter chapter){
        int count = chapterService.updateChapter(chapter);
        return EduResult.ok().data("count",count);
    }
}
